import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import HomePage from '../components/HomePage.vue'
import { getRouter } from '../http/api'

const routes: Array<RouteRecordRaw> = [
  {
    path:'/',
    component:HomePage,
    redirect:'/dashboard',
    children:[
      {
        path:'/dashboard',
        component: () => import('../components/dashboard/Index.vue'),
        name:'dashboard',
        meta:{
          title:'首页',
          chang:'home',
          icon:'#icondashboard'
        }

      }
    ]
  },
  {
    path:'/system',
    component:HomePage,
    name:'system',
    meta:{
      title:'系统管理',
      chang:'system',
      icon:'Menu',
      roles:['sys:manage'],
      parentId:0
    },
    children:[
     {
      path:'/department',
      component: () => import('../views/system/Department/Department.vue'),
      name:'department',
      meta:{
        title:'机构管理',
        chang: 'department',
        icon:'Menu',
        roles:['sys:dept']
      }
     },
      {
        path:'/userList',
        component: () => import('../views/system/User/UserList.vue'),
        name:'userList',
        meta:{
          title:'用户管理',
          chang:'users',
          icon:'Menu',
          roles:['sys:user']
        }
      },
      {
        path:'/roleList',
        component: () => import('../views/system/Role/RoleList.vue'),
        name:'roleList',
        meta:{
          title:'角色管理',
          chang:'roles',
          icon:'Menu',
          roles:['sys:role']
        }
      },
      /* {
        path:'/authority',
        component: () => import('../views/system/Role/AuthorityView.vue'),
        name:'authority',
        meta:{
          title:'权限管理',
          chang:'roles',
        }
      }, */
      {
        path:'/menuList',
        component: () => import('../views/system/Menu/MenuList.vue'),
        name:'menuList',
        meta:{
          title:'权限管理',
          chang:'menuList',
          icon:'Menu',
          roles:['sys:menu']
        }
      }
    ]
  },
  {
    path:'/goods',
    component:HomePage,
    name:'goods',
    meta:{
      title:'商品管理',
      chang:'goods',
      icon:'Menu',
      roles:['sys:goods']
    },
    children:[
      {
        path:'/goodCategory',
        component: () => import('../views/goods/goodsCategory/GoodsCategory.vue'),
        name:'goodCategory',
        meta:{
          title:'商品分类',
          chang:'categories',
          icon:'Menu',
          roles:['sys:goodsCategory']
        }

      }
    ]
  },
  {
    path:'/systemConfig',
    component:HomePage,
    name:'systemConfig',
    meta:{
      title:'系统工具',
      chang:'systenConfig',
      icon:'Menu',
      roles:['sys:systemConfig']
    },
    children:[
      {
        path:'/document',
        component: () => import('../views/system/config/SystemDocument.vue'),
        name:'document',
        meta:{
          title:'接口文档',
          chang:'document',
          icon:'Menu',
          roles:['sys:document']
        }

      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/LoginView.vue')
  }
]
//创建
const router = createRouter({
  history: createWebHashHistory(),
  routes
})

//路由拦截
router.beforeEach(async (to)=>{//同步函数,先用async标记promise，再用await标记函数的调用
  let token:string | null = localStorage.getItem('token')
  if(!token && to.path !== '/dashboard' && to.path !== '/login'){
    return '/login'
  }else if(to.path !== '/login' && token) {
   let routerData:any = await getRouter()
    //动态添加路由
   if(router.getRoutes.length === 6){//判断是否已经添加
    routerData.data.forEach((v:any) => {
      let routerObj:RouteRecordRaw = {//新建并赋值
        path:v.name,
        name:v.name,
        meta:v.meta,
        component: ()=> import(`../views/${v.path}.vue`)
      }
      // router.addRoute("home",routerObj)
    });
   }
  //  router.replace(to.path)
  }

})
export default router
